// ==UserScript==
// @name         测试公式
// @namespace    http://tako.tampermonkey.net/
// @version      0.1
// @description  testFormula
// @author       tako
// @match        *://*/*
// @run-at       context-menu
// @grant        none
// ==/UserScript==
(function () {
  'use strict';
  // Your code here...
  var token = localStorage.getItem('console_TOKEN');
  // console.log(token);

  var formName = document.querySelector('.tfSearchForm .menu-search .ant-input').value;
  var sheetName = document.querySelector('.tfFilterDimension>div>span>input').value;
  var excelKey = document.querySelector('.tfSearchFormula .ant-input').value;

  searchForm();
  //console.log(excelKey);


  function searchForm() {
    var xhr = new XMLHttpRequest();
    xhr.open('GET','/ce/form/searchForm?pageNum=1&pageSize=5&formName='+formName,true);
    xhr.setRequestHeader('Logintoken', token);
    xhr.onreadystatechange = function() {
        if(xhr.readyState == 4 && xhr.status == 200) {
            var data = JSON.parse(xhr.responseText);
            // console.log(data.data.bookInfos);
            var bookId = data.data.bookInfos[0].bookId;
            querySheet(bookId)
        }
    }
    xhr.send();
  }

  function querySheet(bookId) {
    var xhr = new XMLHttpRequest();
    xhr.open('POST','/ce/form/querySheet',true);
    xhr.setRequestHeader('Logintoken', token);
    xhr.onreadystatechange = function() {
        if(xhr.readyState == 4 && xhr.status == 200) {
            var data = JSON.parse(xhr.responseText);
            // console.log(data.data.sheetInfos);
            var sheetId = data.data.sheetInfos[0].sheetId;
            testFormula(bookId,sheetId);
        }
    }
    var param = {};
    param.pageNum=1;
    param.pageSize=5;
    param.pageDimFilter=sheetName;
    param.bookIds=[bookId]
    xhr.send(JSON.stringify(param));
  }

  function testFormula(bookId,sheetId) {
    var xhr = new XMLHttpRequest();
    xhr.open('POST','/ce/test/execMultiExpr',true);
    xhr.setRequestHeader('Logintoken', token);
    xhr.onreadystatechange = function() {
        if(xhr.readyState == 4 && xhr.status == 200) {
            var data = JSON.parse(xhr.responseText);
            console.log(data);
        }
    }
    var param = JSON.parse(prompt('请输入参数:', ''));
    param.formId=bookId;
    param.designMode="0";
    param.sheetId=sheetId;
    param.excelKey=excelKey;
    param.multiExpr=prompt('请输入公式:', '');
    xhr.send(JSON.stringify(param));
  }
})();